package com.power.sifa.std.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.std.api.IStd_admuserFeign;
import com.power.sifa.std.model.Std_admuser;
import com.power.sifa.std.model.pageInfo.Std_admuserPageInfo;
import com.power.sifa.std.service.Std_admuserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   行政管理单位关联赣政通人员表控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Std_admuserController",tags={"行政管理单位关联赣政通人员表"})
public class Std_admuserController implements IStd_admuserFeign {

    @Resource
    private Std_admuserService Std_admuserService;

    
    @Override
    @ApiOperation("查询行政管理单位关联赣政通人员表分页列表")
    @SaCheckPermission("Std_admuser_list")
    public ResponseDTO list(Std_admuserPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Std_admuser> page = this.Std_admuserService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Std_admuser Std_admuser : page) {
                Std_admuser.geneText();
                list.add(Std_admuser);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看行政管理单位关联赣政通人员表")
    @SaCheckPermission("Std_admuser_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Std_admuser Std_admuser  = Std_admuserService.getById(id);
        if (Std_admuser != null) {
            Std_admuser.geneText();
        }
        return ResponseDTO.succData(Std_admuser);
    }

    @Override
    @ApiOperation("删除行政管理单位关联赣政通人员表")
    @SaCheckPermission("Std_admuser_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        Std_admuserService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改行政管理单位关联赣政通人员表")
    @SaCheckPermission("Std_admuser_save")
    public ResponseDTO save(Std_admuser Std_admuser) {
        Assert.notNull(Std_admuser, "Std_admuser不能为空");
        Std_admuserService.saveOrUpdate(Std_admuser);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除行政管理单位关联赣政通人员表")
    @SaCheckPermission("Std_admuser_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        Std_admuserService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("Std_admuser_list")
    public void output(HttpServletResponse response, Std_admuserPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "行政管理单位关联赣政通人员表";
        String[] heads = {"序号","关联表id","赣政通人员id","赣政通人员名称","赣政通组织机构id","赣政通人员手机号码"};
        String[] fields = {"id","admunitid","userid","username","unitid","mobile"};
        Page<Std_admuser> page = this.Std_admuserService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "行政管理单位关联赣政通人员表";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","关联表id","赣政通人员id","赣政通人员名称","赣政通组织机构id","赣政通人员手机号码"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","admunitid","userid","username","unitid","mobile"};
        List<Std_admuser> list = (List<Std_admuser>)ExcelUtil.Import(fields, file, Std_admuser.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.Std_admuserService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO checkSave(Std_admuser stdAdmuser) {
        this.Std_admuserService.checkSave(stdAdmuser);
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO listDetail(Std_admuserPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        return ResponseDTO.succData(this.Std_admuserService.listDetail(pageInfo));
    }

    @Override
    public ResponseDTO queryByAdmId(String admId) {
        Assert.notNull(admId, "admId参数不能为空");
        List<Std_admuser> admuserList = this.Std_admuserService.queryByAdmId(admId);
        return ResponseDTO.succData(admuserList);
    }

}
